package com.sysu.ooad.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.Action;
import com.sysu.ooad.entity.Course;
import com.sysu.ooad.entity.Group;
import com.sysu.ooad.entity.Student;
import com.sysu.ooad.entity.Teacher;
import com.sysu.ooad.service.CourseService;
import com.sysu.ooad.service.GroupService;
import com.sysu.ooad.service.StudentService;

@Component
@Scope("prototype")
public class GroupManageTeacherAction extends CommonAction{
	
	private CourseService courseService;
	private GroupService groupService;
	private StudentService studentService;
	private List<Group> groupList;
	private List<Student> freeStudents;
	private Group group;
	
	public CourseService getCourseService() {
		return courseService;
	}
	@Resource
	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	public List<Student> getFreeStudents() {
		return freeStudents;
	}
	public void setFreeStudents(List<Student> freeStudents) {
		this.freeStudents = freeStudents;
	}
	public List<Group> getGroupList() {
		return groupList;
	}
	public void setGroupList(List<Group> groupList) {
		this.groupList = groupList;
	}
	public GroupService getGroupService() {
		return groupService;
	}
	
	public StudentService getStudentService() {
		return studentService;
	}
	@Resource
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	@Resource
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}
	
	public Teacher getCurrentTeacher() {
		return (Teacher)session.get("teacher");
	}
	
	public String execute() throws Exception {
		Teacher t = this.findCurrentTeacher();
		groupList = groupService.findByCourse(t.getCourse());
		freeStudents = studentService.findWhoHaveNoGroup(t.getCourse());
		return Action.SUCCESS;
	}
	
	public String lockGroup() {
		Teacher t = this.getCurrentTeacher();
		Group g = groupService.getById(group.getId());
		groupService.lock(g, group.isLocked());
		return null;
	}
	
	public String lockAllGroups() {
		Teacher t = this.findCurrentTeacher();
		Course c = t.getCourse();
		courseService.lock(c, !c.isLocked());
		t.setCourse(c);
		return Action.SUCCESS;
	}
}
